Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

230
Visualizações
Executing same query gives "FailedToParse" error in nodejs

Database structure is;

{ "key" : "test" }
{ "key" : "test" }
{ "key" : "test" }
{ "key" : "test" }

In mongoshell the query,

db.collectionName.find({}, {key: 1, _id: 0})

lists the documents.

However, when I run the same query in nodejs it gives me an error as;

{
  "name": "MongoError",
  "message": "Failed to parse: comment: 1. 'key' field must be of BSON type string.",
  "ok": 0,
  "errmsg": "Failed to parse: comment: 1. 'key' field must be of BSON type string.",
  "code": 9,
  "codeName": "FailedToParse"
}

I don't understand what can be the problem?

I am aware that the question doesn't make sense, however,meaninglessly this is what I have. Is it about mongo versions or what?

edit: You are right about writing Node.js code, so here it is, the query inside an async.series call;

async.series([
  function(callback){
    db.collection("collectionName").find({}, {key: 1, _id: 0}).toArray(function(err, result){
        if(err){
            callback(err);
        } else{
            callback(null, result);
        }
    });
   }
])
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

I don't know why you did include the mongo shell line that worked but didn't include the Node line that failed - which would be much more relevant here.

In any case, the native Mongo driver for Node (which I assume you're using here) works in a completely different way than the Mongo shell so don't expect it to work the same.

Mongo shell executes everything sequentially. In Node you have to use callbacks or promises for flow control which you don't do in the Mongo shell. Also there are some differences in how you access the collections. In the Mongo shell you usually use db.collectionName. Also in the Mongo shell you have some things predefined, like functions related to object ids for example. And finally Mongo uses BSON for data and in Node you use JavaScript object literals (more like JSON but less strict). Node uses V8 plus additional functionality and Mongo shell uses SpiderMonkey, with no functionality that is available in Node. It all have consequences in subtle differences.

You didn't include the Node line that failed so no one will be able to fix that line. But you certainly need to take a look at it and see if you're following the documentation of whetever Mongo driver or ODM you're using.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda